package com.kingyun.gpsinspection.purificationservice.services.iam.outerservice.adm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.AutoApplyMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.AutoApply;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.TaskLinkIam;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.accessory.BPMService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.OutAutoApplyService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.TaskLinkIamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.net.URLEncoder;

/**
 * Created by yangsy on 2016/12/16.
 */
@Service
public class OutAutoApplyServiceImpl implements OutAutoApplyService{
    @Autowired
    private AutoApplyMapper autoApplyMapper;
    @Autowired
    private TaskLinkIamService taskLinkIamService;
    @Autowired
    private BPMService bpmService;
    @Override
    public int deleteByPrimaryKey(String autoApplyId) {
        return autoApplyMapper.deleteByPrimaryKey(autoApplyId);
    }

    /**
     * 新增并发起流程
     * @param record
     * @param username
     * @param password
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertSelective(AutoApply record,String username, String password) {
        int count =0;
        //开启流程
        String ret= bpmService.openBPM(2,username,password,null);
        //保存任务环节
        TaskLinkIam taskLink=  new TaskLinkIam();
        taskLinkIamService.save(taskLink,ret,(short)2,record.getAutoApplyId(),(short)1);
        //完成流程
        bpmService.completeTask(taskLink.getTkiid(), username, password, null);
        count += autoApplyMapper.insertSelective(record);
        return count;
    }

    @Override
    public AutoApply selectByPrimaryKey(String autoApplyId) {
        return autoApplyMapper.selectByPrimaryKey(autoApplyId);
    }

    @Override
    public JSONObject list(AutoApply autoApply) {
        JSONObject jsonObject = new JSONObject();
        autoApply.setLimit( autoApply.getLimit() == null ? Integer.valueOf(10) : autoApply.getLimit() );
        autoApply.setOffset( autoApply.getOffset() == null ? Integer.valueOf(0) : autoApply.getOffset() );
        jsonObject.put("total",autoApplyMapper.queryCount(autoApply));
        jsonObject.put("rows", JSON.toJSON(autoApplyMapper.queryList(autoApply)));
        return jsonObject;
    }

    /**
     * 节点处理
     * @param autoApply
     * @param taskLinkIam
     * @param userName
     * @param pwd
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int dispose(AutoApply autoApply, TaskLinkIam taskLinkIam, String userName, String pwd){
        //用户信息
        String username = userName;
        String password = pwd;
        short note=1;
        String parameter=null;
        int retCount=0;
        try{
            //判断节点位置
            if (taskLinkIam.getRole().equals("部门经理")) {
                note = 2;
                String statue = autoApply.getStatus().toString();
                //流程走向参数
                parameter = "{'isagree':'" + statue + "'}";
                //转换成utf-8
                parameter = URLEncoder.encode(parameter, "utf-8");
                //设置任务环节审批状态
                taskLinkIam.setAuditStatus(autoApply.getStatus());
                //设置任务环节备注
                taskLinkIam.setRemark(autoApply.getComments());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //处理流程
        String result = bpmService.completeTask(taskLinkIam.getTkiid(), username, password, parameter);
        //保存任务环节
        taskLinkIamService.save(taskLinkIam, result, (short)2, autoApply.getAutoApplyId(), note);
        //保存业务数据
        retCount +=autoApplyMapper.updateByPrimaryKeySelective(autoApply);
        return retCount;
    }
    /**
     * 查询代处理
     * @param bootstrapSearchBean
     * @param task
     * @param type 0 休假1出差2用车
     * @return
     * @throws Exception
     */
    @Override
    public JSONObject listToDoTask( BootstrapSearchBean bootstrapSearchBean, TaskLinkIam task, int type,String username,String passwor){
        JSONObject jsonObject = new JSONObject();
        try{
            jsonObject = taskLinkIamService.listToDoTask(bootstrapSearchBean,task,type, username, passwor);
        }catch(Exception e){
            e.printStackTrace();
        }
        return  jsonObject;
    }
}
